package com.dlws.modules.monitoring.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dlws.common.utils.Constants;
import com.dlws.common.utils.DateUtils;
import com.dlws.common.web.BaseController;
import com.dlws.modules.audio.entity.AudioInfo;
import com.dlws.modules.audio.service.AudioInfoService;
import com.dlws.modules.monitoring.entity.ComputerInfo;
import com.dlws.modules.monitoring.entity.ComputerInfoPageVo;
import com.dlws.modules.monitoring.service.ComputerService;
import com.dlws.modules.monitoring.util.TreeNode;
import com.page.Pagination;

@Controller
@RequestMapping(value = "/monitoring")
public class MachineRoomController extends BaseController {

	@Autowired
	private ComputerService computerService;
	@Autowired
	private AudioInfoService audioInfoService;

	/**
	 * 
	 * 描述：进入到机房信息页面 
	 * 创建人： 高磊 
	 * 创建时间: 2016-6-1 上午11:24:59
	 * 
	 * @version
	 */
	@RequestMapping(value = "/computerList")
	public String computerList(ModelMap model, Integer pageNo,
			HttpServletRequest request, String searchName, Integer searchType) {
		logger.info("--进入机房信息列表方法--产品模糊查询--分页--");

		/*
		 * ComputerInfo computerInfo1= new ComputerInfo();
		 * computerInfo1.setComPid(0); List<ComputerInfo> queryAll
		 * =computerService.queryAll(computerInfo1); model.put("list",
		 * queryAll);
		 */
		// 参数
		StringBuilder params = new StringBuilder();
		ComputerInfoPageVo computerInfo = new ComputerInfoPageVo();
		// 如果页号为null 或小于1 设为1
		computerInfo.setPageNo(Pagination.cpn(pageNo));
		computerInfo.setPageSize(Constants.PAGE_SIZE);
		// computerInfo.setPageSize(2);
		computerInfo.setComPid(0);
		Pagination pagination = computerService.pagelist(computerInfo);

		// 分页展示
		String url = request.getContextPath() + "/monitoring/computerList.do";
		pagination.pageView(url, params.toString());
		model.put("page", pagination);
		return "/monitoring/list";

	}

	/**
	 * 
	 * 描述：查看详情
	 * 创建人： 高磊
	 * 创建时间: 2016-6-3 下午1:32:56
	 * @version
	 */
	@RequestMapping(value = "/computerListbyPid")
	public String computerListbyPid(ModelMap model, Integer pageNo,
			HttpServletRequest request, String id) {
		logger.info("--进入机房信息列表方法--产品模糊查询--分页--");
		/*
		 * ComputerInfo computerInfo1 = new ComputerInfo();
		 * computerInfo1.setComPid(Integer.valueOf(id)); List<ComputerInfo>
		 * queryAll = computerService.queryAll(computerInfo1); model.put("list",
		 * queryAll);
		 */
		StringBuilder params = new StringBuilder();
		
		if(StringUtils.isNotBlank(id)){
			params.append("&id=").append(id);
		}
		
		ComputerInfoPageVo computerInfo = new ComputerInfoPageVo();
		// 如果页号为null 或小于1 设为1
		computerInfo.setPageNo(Pagination.cpn(pageNo));
		computerInfo.setPageSize(Constants.PAGE_SIZE);
		computerInfo.setIsDelete(0);
		computerInfo.setIsUse(1);
		computerInfo.setComPid(Integer.valueOf(id));
		Pagination pagination = computerService.pagelist(computerInfo);

		// 分页展示
		String url = request.getContextPath()
				+ "/monitoring/computerListbyPid.do";
		pagination.pageView(url, params.toString());
		model.put("page", pagination);

		return "/monitoring/listbypid";

	}

	/**
	 * 
	 * 描述：进入添加页面 
	 * 创建人： 高磊
	 * 创建时间: 2016-6-1 下午2:47:35
	 * 
	 * @version
	 */
	@RequestMapping(value = "/add")
	public String add() {
		return "/monitoring/add";

	}

	/**
	 * 
	 * 描述：添加机房所在城市
	 * 创建人： 高磊 
	 * 创建时间: 2016-5-31 下午5:10:40
	 * 
	 * @version
	 */
	@RequestMapping(value = "/computerAdd")
	public String computerAdd(ComputerInfo computerInfo) {
		computerInfo.setCreateTime(DateUtils.getCurrentDate());
		computerInfo.setComPid(0);
		computerService.addSelective(computerInfo);
		return "redirect:/monitoring/computerList.do";

	}

	/**
	 * 
	 * 描述：机房所在城市删除状态更改
	 * 创建人： 高磊
	 * 创建时间: 2016-6-1 下午4:27:10
	 * @version
	 */

	@RequestMapping(value = "/computerDel")
	public String computerDel(ComputerInfo computerInfo) {
		if (computerInfo.getIsDelete() == 0) {
			// 0删除，1不删除
			computerInfo.setIsDelete(1);
		} else {
			computerInfo.setIsDelete(0);
		}
		computerService.updateByPrimaryKeySelective(computerInfo);
		return "redirect:/monitoring/computerList.do";

	}

	/**
	 * 
	 * 描述：机房所在城市更改启用状态
	 * 创建人： 高磊
	 * 创建时间: 2016-6-1 下午4:36:14
	 * @version
	 */
	@RequestMapping(value = "/computerUp")
	public String ComputerUp(ComputerInfo computerInfo) {
		if (computerInfo.getIsUse() == 0) {
			// 0不启用，1启用
			computerInfo.setIsUse(1);
		} else {
			computerInfo.setIsUse(0);
		}
		computerService.updateByPrimaryKeySelective(computerInfo);
		return "redirect:/monitoring/computerList.do";

	}

	/**
	 * 
	 * 描述：进入添加机房所在地页面
	 * 创建人： 高磊
	 * 创建时间: 2016-6-1 下午4:52:15
	 * @version
	 */
	@RequestMapping(value = "/addComputerInfo")
	public String addComputerInfo(String id, ModelMap model) {
		model.put("pid", id);
		return "/monitoring/addcomputerinfo";

	}

	/**
	 * 
	 * 描述：添加机房所在地
	 * 创建人： 高磊
	 * 创建时间: 2016-6-3 下午1:38:10
	 * @version
	 */
	@RequestMapping(value = "/addLocation")
	public String addLocation(ComputerInfo computerInfo) {
		computerInfo.setCreateTime(DateUtils.getCurrentDate());
		computerService.addSelective(computerInfo);
		return "redirect:/monitoring/computerListbyPid.do?id="
				+ computerInfo.getComPid();

	}

	/**
	 * 
	 * 描述：机房所在地删除状态更改
	 * 创建人： 高磊
	 * 创建时间: 2016-6-1 下午4:27:10
	 * @version
	 */

	@RequestMapping(value = "/computerDelete")
	public String computerDelete(ComputerInfo computerInfo) {
		if (computerInfo.getIsDelete() == 0) {
			// 0删除，1不删除
			computerInfo.setIsDelete(1);
		} else {
			computerInfo.setIsDelete(0);
		}
		computerService.updateByPrimaryKeySelective(computerInfo);
		return "redirect:/monitoring/computerListbyPid.do?id="
				+ computerInfo.getComPid();

	}

	/**
	 * 
	 * 描述：机房所在地更改启用状态
	 * 创建人： 高磊
	 * 创建时间: 2016-6-1 下午4:36:14
	 * @version
	 */
	@RequestMapping(value = "/computerUpdate")
	public String ComputerUpdate(ComputerInfo computerInfo) {
		if (computerInfo.getIsUse() == 0) {
			// 0不启用，1启用
			computerInfo.setIsUse(1);
		} else {
			computerInfo.setIsUse(0);
		}
		computerService.updateByPrimaryKeySelective(computerInfo);
		return "redirect:/monitoring/computerListbyPid.do?id="
				+ computerInfo.getComPid();

	}

	/**
	 * 
	 * 描述：机房ztrees树
	 * 创建人： 高磊
	 * 创建时间: 2016-6-7 下午5:59:56
	 * @version
	 */
	@RequestMapping(value = "/treeList")
	public void treeList(HttpServletResponse response) {
		ComputerInfo computerInfo = new ComputerInfo();
		// computerInfo.setComPid(0);
		computerInfo.setIsDelete(0);
		computerInfo.setIsUse(1);
		List<ComputerInfo> queryAll = computerService.queryAll(computerInfo);
		List<TreeNode> list = new ArrayList<TreeNode>();
		for (ComputerInfo computerInfo2 : queryAll) {
			TreeNode node = new TreeNode(computerInfo2.getId(),
					computerInfo2.getComPid(), computerInfo2.getComAddress(),
					false, true);
			list.add(node);
		}
		printJSON(response, list);
	}

	/**
	 * 
	 * 描述：机房树页面
	 * 创建人： 高磊
	 * 创建时间: 2016-6-16 上午10:35:40
	 * @version
	 */
	@RequestMapping(value = "/toTreeList")
	public String toTreeList() {
		return "/monitoring/treelist";
	}

	/**
	 * 
	 * 描述：查询信号源
	 * 创建人： 高磊
	 * 创建时间: 2016-6-15 下午4:19:25
	 * @version
	 */
	@ResponseBody
	@RequestMapping(value = "/audioListbyOid")
	public Map<String, Object> AudioListByOid(String compid) {
		Map<String, Object> map = new HashMap<String, Object>();
		AudioInfo audioInfo = new AudioInfo();
		audioInfo.setIsDelete(0);
		audioInfo.setIsUse(1); 
		audioInfo.setComId(Integer.valueOf(compid));
		List<AudioInfo> queryAll = audioInfoService.queryAll(audioInfo);
		map.put("list", queryAll);
		return map;
	}
}
